package com.ruoyi.web.controller.admin;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.BdmRoyaltyRecord;
import com.ruoyi.system.domain.BdmShareOutBonusLevel;
import com.ruoyi.system.domain.dto.BdmReceiveAddressDto;
import com.ruoyi.system.domain.dto.BdmShareOutBonusLevelDto;
import com.ruoyi.system.domain.vo.BdmUserVo;
import com.ruoyi.system.enums.BdmRoyaltyRecordEnum;
import com.ruoyi.system.service.IBdmRoyaltyRecordService;
import com.ruoyi.system.service.IBdmShareOutBonusLevelService;
import com.ruoyi.system.service.IBdmUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "后台-奖金池管理")
@RestController
@RequestMapping("/admin/ShareOutBonus")
public class BdmShareOutBonusController extends BaseController {


    @Autowired
    private IBdmUserService iBdmUserService;

    @Autowired
    private IBdmRoyaltyRecordService iBdmRoyaltyRecordService;

    @Autowired
    private IBdmShareOutBonusLevelService iBdmShareOutBonusLevelService;

    /**
     * 查询分红池列表
     */
    @GetMapping("/getShareOutBonusLevelPage")
    public TableDataInfo getShareOutBonusLevelPage() {
        startPage();
        List<BdmShareOutBonusLevel> list = iBdmShareOutBonusLevelService.lambdaQuery().list();
        BdmShareOutBonusLevel bdmShareOutBonusLevel = list.get(0);
        Map<String, String> params = new HashMap<>();
        List<BdmUserVo> svipPage = iBdmUserService.getSvipPage(params);
        int size = svipPage.size();
        bdmShareOutBonusLevel.setNumberOfPeople(size);
        return getDataTable(list);
    }

    @PostMapping("/updateBonusStatus")
    public AjaxResult updateBonusStatus(@RequestBody BdmUserVo bdmUserVo) {
        return iBdmUserService.updateBonusStatus(bdmUserVo.getSysUserid(), bdmUserVo.getBonusStatus());
    }

    /**
     * 新增分红池
     */
    @PostMapping("/saveShareOutBonusLevel")
    public AjaxResult saveShareOutBonusLevel(@RequestBody BdmShareOutBonusLevelDto dto) {
        return success(iBdmShareOutBonusLevelService.saveShareOutBonusLevel(dto));
    }

    /**
     * 获取分红池详情
     */
    @GetMapping("/getShareOutBonusLevelById")
    public AjaxResult getShareOutBonusLevelById(BdmShareOutBonusLevelDto dto) {
        return success(iBdmShareOutBonusLevelService.getShareOutBonusLevelById(dto.getId()));
    }

    /**
     * 修改分红池
     */
    @PostMapping("/updateShareOutBonusLevel")
    public AjaxResult updateShareOutBonusLevel(@RequestBody BdmShareOutBonusLevelDto dto) {
        return success(iBdmShareOutBonusLevelService.updateShareOutBonusLevel(dto));
    }

    /**
     * 删除分红池
     */
    @GetMapping("/deleteShareOutBonusLevelById/{id}")
    public AjaxResult deleteShareOutBonusLevelById(@PathVariable("id") Long id) {
        return success(iBdmShareOutBonusLevelService.removeById(id));
    }

    /**
     * 充值分红池
     */
    @GetMapping("/topUpShareOutBonusLevel")
    public AjaxResult topUpShareOutBonusLevel(@RequestParam("id") Long id, @RequestParam("money") String money) {
        BdmShareOutBonusLevel share = iBdmShareOutBonusLevelService.getById(id);
        if (share == null || share.getStatus() == 2) {
            throw new RuntimeException("当前分红池已关闭,不能充值");
        }
        BigDecimal zero = BigDecimal.ZERO;
        try {
            BigDecimal bigDecimal = new BigDecimal(money);
            zero = zero.add(bigDecimal);
        } catch (Exception e) {
            throw new RuntimeException("请输入数字");
        }
        return success(iBdmShareOutBonusLevelService.lambdaUpdate().eq(BdmShareOutBonusLevel::getId, id).set(BdmShareOutBonusLevel::getAtPresentMoney, share.getAtPresentMoney().add(zero)).update());
    }

    /**
     * 查询分红池用户
     */
    @GetMapping("/getSvipPage")
    public TableDataInfo getSvipPage(BdmReceiveAddressDto dto) {
        startPage();
        Map<String, String> params = new HashMap<>();
        params.put("userPhone", dto.getUserPhone());
        params.put("sysUserId", dto.getSysUserId());
        return getDataTable(iBdmUserService.getSvipPage(params));
    }

    /**
     * 根据用户id查询用户分红记录
     *
     * @param sysUserId
     * @return
     */
    @GetMapping("/getUserShareOutBonusPage")
    public TableDataInfo getUserShareOutBonusPage(@RequestParam("sysUserId") Long sysUserId) {
        startPage();
        BdmRoyaltyRecord record = new BdmRoyaltyRecord();
        record.setSysUserid(sysUserId);
        record.setType(BdmRoyaltyRecordEnum.DIVIDEND_AWARD.getType());
        return getDataTable(iBdmRoyaltyRecordService.selectBdmRoyaltyRecordList(record));
    }
}
